#!/bin/bash

DATE=`date +%Y%m%d%H%M%S`
# echo $DATE

# 备份jar
function backup(){
  if [ -f "/home/www/jar/blog.jar" ]; then
    echo "/home/www/jar/blog.jar已存在"
    mv  /home/www/jar/blog.jar  /home/www/jar/backup/blog-$DATE.jar
    echo "备份完毕"
  else
    echo "/home/www/jar/blog.jar不存在"
  fi

  move
}

# 移动jar至工作目录
function move(){
  if [ -f "/home/www/blog.jar" ]; then
    echo "/home/www/blog.jar已存在"
    mv /home/www/blog.jar /home/www/jar/
    echo "复制成功"
  else
    echo "/home/www/blog.jar不存在"
  fi
}

# 停止服务
function stop() {
 echo "Stopping blog ..."
  pid=$(ps -ef|grep -i blog.jar |grep -v "grep"|awk '{print $2}')
  echo "获取blog pid成功 "+$pid
  if [ -n "$pid" ];
   then kill -9 $pid
   echo '解除端口占用'
  fi
}



# 启动服务
function start() {
  echo "Starting blog ..."
  /usr/bin/java -Dserver.port=8000 -Dspring.profiles.active=prod -server -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=512m -Xms1024m -Xmx1024m -Xmn256m -Xss256k -XX:SurvivorRatio=8 -jar /home/www/jar/blog.jar  > /home/www/jar/logs/blog.log 2>&1 &
  echo "启动服务"
}

# 运行
function run() {
  if [ -f "/home/www/blog.jar" ]; then
    stop
    backup
    start
  else
    echo "新JAR包不存在【/home/www/blog.jar】"
  fi
}

# 入口
run
